{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f2d5e279-1cbc-4291-985f-9e23af3a6ecc",
   "metadata": {},
   "outputs": [],
   "source": [
    "import torch\n",
    "from gliner import GLiNER"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "3baf9b40-daba-4638-b4a2-cc82c8a9ed99",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# first load your model\n",
    "\n",
    "model = GLiNER.from_pretrained(\"gliner-community/gliner_medium-v2.5\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5351bc8d-1182-4398-8be7-de61e6b24936",
   "metadata": {},
   "source": [
    "## Option 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "c25fbf7b-b10c-4808-995e-2431f6c0356f",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save\n",
    "\n",
    "model.save_pretrained(\"gliner_Med\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "f46d16c1-ab18-4300-b21f-e78c1da81df3",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# load\n",
    "\n",
    "loaded_model = GLiNER.from_pretrained(\"gliner_Med\", load_tokenizer = True, local_files_only=True)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "4041910a-ee0e-470d-b718-bc151a2666eb",
   "metadata": {},
   "source": [
    "## Option 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "fe7e3b71-1d15-4739-9b41-fcc279046950",
   "metadata": {},
   "outputs": [],
   "source": [
    "def save_model(current_model, path):\n",
    "    config = current_model.config\n",
    "    dict_save = {\"model_weights\": current_model.state_dict(), \"config\": config}\n",
    "    torch.save(dict_save, path)\n",
    "\n",
    "\n",
    "def load_model(path, model_name=None):\n",
    "    \n",
    "    dict_load = torch.load(path, map_location=torch.device('cpu'))\n",
    "    config = dict_load[\"config\"]\n",
    "\n",
    "    print(f\"'{config.model_name}' should be available for local processing\")\n",
    "\n",
    "    if model_name is not None:\n",
    "        config.model_name = model_name\n",
    "\n",
    "    loaded_model = GLiNER(config)\n",
    "    loaded_model.load_state_dict(dict_load[\"model_weights\"])\n",
    "    return loaded_model"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "e513be85-3178-449c-adec-1a609e38b580",
   "metadata": {},
   "outputs": [],
   "source": [
    "# save the model weight\n",
    "\n",
    "save_model(model, \"model_weight.pt\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "628eb872-ff3d-4c59-ac20-9b229797090f",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "# load model weight\n",
    "\n",
    "loaded_model = load_model(\"model_weight.pt\")\n",
    "print(\"success !!\")"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "e057d7ec-1756-4c97-a1d9-e5fdcb60e20a",
   "metadata": {},
   "source": [
    "## Testing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2827009e-bdb8-44b2-92b5-e6bdcc17f08e",
   "metadata": {},
   "outputs": [],
   "source": [
    "text = \"\"\"\n",
    "Libretto by Marius Petipa, based on the 1822 novella ``Trilby, ou Le Lutin d'Argail`` by Charles Nodier, first presented by the Ballet of the Moscow Imperial Bolshoi Theatre on January 25/February 6 (Julian/Gregorian calendar dates), 1870, in Moscow with Polina Karpakova as Trilby and Ludiia Geiten as Miranda and restaged by Petipa for the Imperial Ballet at the Imperial Bolshoi Kamenny Theatre on January 17–29, 1871 in St. Petersburg with Adèle Grantzow as Trilby and Lev Ivanov as Count Leopold.\n",
    "\"\"\"\n",
    "\n",
    "labels = [\"person\", \"book\", \"location\", \"date\", \"actor\", \"character\"]\n",
    "\n",
    "entities = loaded_model.predict_entities(text, labels, threshold=0.4)\n",
    "\n",
    "for entity in entities:\n",
    "    print(entity[\"text\"], \"=>\", entity[\"label\"])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "839336f8-e5a0-471d-ace9-ef7f7e1c5c97",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
